package org.news.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.HistoryVo;
import org.news.web.domain.bo.HistoryBo;
import org.news.web.service.IHistoryService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 历史记录
 *
 * @author Gu Ci
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/history")
public class HistoryController extends BaseController {

    private final IHistoryService historyService;

    /**
     * 查询历史记录列表
     */
    @SaCheckPermission("web:history:list")
    @GetMapping("/list")
    public TableDataInfo<HistoryVo> list(HistoryBo bo, PageQuery pageQuery) {
        return historyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出历史记录列表
     */
    @SaCheckPermission("web:history:export")
    @Log(title = "历史记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HistoryBo bo, HttpServletResponse response) {
        List<HistoryVo> list = historyService.queryList(bo);
        ExcelUtil.exportExcel(list, "历史记录", HistoryVo.class, response);
    }

    /**
     * 获取历史记录详细信息
     *
     * @param historyId 主键
     */
    @SaCheckPermission("web:history:query")
    @GetMapping("/{historyId}")
    public R<HistoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long historyId) {
        return R.ok(historyService.queryById(historyId));
    }

    /**
     * 新增和修改历史记录
     */
    @SaCheckPermission("web:history:add")
    @Log(title = "历史记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HistoryBo bo) {
        return toAjax(historyService.insertByBo(bo));
    }

    /**
     * 修改历史记录
     */
    @SaCheckPermission("web:history:edit")
    @Log(title = "历史记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HistoryBo bo) {
        return toAjax(historyService.updateByBo(bo));
    }

    /**
     * 删除历史记录
     *
     * @param historyIds 主键串
     */
    @SaCheckPermission("web:history:remove")
    @Log(title = "历史记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{historyIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] historyIds) {
        return toAjax(historyService.deleteWithValidByIds(List.of(historyIds), true));
    }
}
